import router from './router'
import store from '@/store'

//路由拦截
router.beforeEach(async (to, from, next) => {
    //1、是否是登录页
    if (to.path == '/login') {
        next();
    } else {
        //获取角色信息
        let R = store && store.getters.roles && store.getters.roles.length > 0;
        if (R) {
            next();
        } else {
            //没有角色
            //获取角色 发送请求
            let {roles} = await store.dispatch('USERINFO')
            // 通过角色过滤菜单数据
            let _routes = await store.dispatch('FILTERMENU', roles)

            router.addRoutes(_routes); //动态路由数据添加
            if (roles) {  //判断是否有角色
                next({path: to.path});
            } else {
                next({path: '/login'});
            }
        }
    }

})